home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / examples / ex_5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  11.0 KB  |  222 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "colors.h"
  4. #include "!bestlib.h"
  5.  
  6.    /*** NOTE  the names and structure of the routines used in these examples
  7.               have changed in The Best Library 2.00; the updated examples will
  8.               be released soon                                            ***/
  9.  
  10. #define ERR01 0
  11. #define MSG01 1
  12. #define MSG02 2
  13. #define MSG03 3
  14. #define MSG04 4
  15. #define MSG05 5
  16. #define MSG06 6
  17. #define MSG07 7
  18. #define MSG08 8
  19. #define MSG09 9
  20. #define MSG0A 10
  21. #define MSG0B 11
  22. #define MSG0C 12
  23. #define LOC1 7
  24. #define LOC2 11
  25. #define LOC3 5
  26. #define LOC4 0
  27. #define LOC5 24
  28. #define QUE01 13
  29. #define QUE02 14
  30. #define QUE03 15
  31. #define TXT01 16
  32.  
  33. void floodtext(void);
  34. void showtext(void);
  35.  
  36. filldata  fidata = { GREEN, BLACK, 0, 0, 0, 80, 25, 32, 0, "" };
  37. printdata prdata = { -1, -1, -1, 0, 0, "" };
  38. mousedata msdata;
  39. asciiscan keyp;
  40.  
  41. char buffer[70];
  42. char *text[] = {
  43.    "!!! ERROR OCCURRED INITIALIZING THE MOUSE DRIVER !!!",        /* ERR01 */
  44.    "Demonstration of Microsoft compatible mouse routines",        /* MSG01 */
  45.    "Authored independently by George Vanous",                     /* MSG02 */
  46.    "left button is not pressed",                                  /* MSG03 */
  47.    "left button is pressed    ",                                  /* MSG04 */
  48.    "left button was pressed at",                                  /* MSG05 */
  49.    "left button was released at",                                 /* MSG06 */
  50.    "right button is not pressed",                                 /* MSG07 */
  51.    "right button is pressed    ",                                 /* MSG08 */
  52.    "right button was pressed at",                                 /* MSG09 */
  53.    "right button was released at",                                /* MSG0A */
  54.    " cursor is located at",                                       /* MSG0B */
  55.    "press any key to terminate",                                  /* MSG0C */
  56.    "Do you wish to continue regardless?",                         /* QUE01 */
  57.    "Which ASCII character to use as cursor? [219]",               /* QUE02 */
  58.    "What color? [7]",                                             /* QUE03 */
  59.    "ASCII table",                                                 /* TXT01 */
  60. };
  61.  
  62. void main(void)
  63. {
  64.    int chart, color, oldmode, z;
  65.  
  66.    if (!initms()) {                    /* if the mouse could not be found   */
  67.       cursorwhere(&prdata.x);          /* stores the position of the cursor */
  68.       prdata.string2print = text[ERR01];
  69.       print();                         /* print the error message           */
  70.       prdata.string2print = text[QUE01];
  71.       prdata.y++;
  72.       print();                         /* print the question                */
  73.       if (upcase(getchre()) != 'Y')    /* if the user response was not Yes  */
  74.          exit(1);                      /* exit to DOS with errorlevel 1     */
  75.    }
  76.    oldmode = readvideomode();          /* save the current video mode       */
  77.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  78.       textmem(3);   /* if old mode was a text mode, store text video memory */
  79.       cursor(3, 1);                    /* hide and store cursor position    */
  80.    }
  81.    textm(1);                          /* change to 80x25x16 color text mode */
  82.    clear(LIGHTGRAY, BLACK);            /* clear the screen and set colors   */
  83.  
  84.    prdata.x = 5, prdata.y = 11;        /* set the next print position       */
  85.    prdata.string2print = text[QUE02];
  86.    print();                            /* print the question                */
  87.    z = readnumber(255, 1, prdata.x + stringlen(prdata.string2print)+1,
  88.                   prdata.y, &chart);   /* get the user's input              */
  89.    if (z == FALSE) chart = 219;        /* if <ENTER> or <ESC>, use default  */
  90.    prdata.y += 2;                      /* set the next print position       */
  91.    prdata.string2print = text[QUE03];
  92.    print();                            /* print the question                */
  93.    z = readnumber(15, 0, prdata.x + stringlen(prdata.string2print)+1,
  94.                   prdata.y, &color);   /* get the user's input              */
  95.    if (z == FALSE) color = 7;          /* if <ENTER> or <ESC>, use default  */
  96.  
  97.    clear(LIGHTGRAY, BLACK);            /* clear the screen and set colors   */
  98.    showtext();                         /* print up the screen               */
  99.    prdata.y = LOC1;                    /* define the y-coordinate to print  */
  100.    prdata.command = -1;                /* specify no modifications          */
  101.    prdata.string2print = text[MSG03];
  102.    print();                            /* print left mouse button status    */
  103.    prdata.y = LOC2;                    /* define the y-coordinate to print  */
  104.    prdata.string2print = text[MSG07];
  105.    print();                            /* print right mouse button status   */
  106.    shapems(chart, color, -1);          /* change the mouse cursor and color */
  107.    showms();                           /* display the mouse cursor          */
  108.    stopw(0, 15);                       /* start stopwatch 0                 */
  109.  
  110.    while (!keyhit()) {
  111.       floodtext();                     /* fill the entire screen            */
  112.       statms();                        /* read the mouse status             */
  113.       if (msdata.pos[0] != msdata.npos[0]) {
  114.          msdata.pos[0] = msdata.npos[0];
  115.          msdata.update += 1;           /* if the mouse x-position changed.. */
  116.       }
  117.       if (msdata.pos[1] != msdata.npos[1]) {
  118.          msdata.pos[1] = msdata.npos[1];
  119.          msdata.update += 2;           /* if the mouse y-position changed.. */
  120.       }
  121.       if (msdata.buts[0] != msdata.nbuts[0]) {
  122.          msdata.buts[0] = ~msdata.buts[0];
  123.          msdata.update += 4;    /* if the left mouse button state changed.. */
  124.       }
  125.       if (msdata.buts[1] != msdata.nbuts[1]) {
  126.          msdata.buts[1] = ~msdata.buts[1];
  127.          msdata.update += 8;   /* if the right mouse button state changed.. */
  128.       }
  129.       if (msdata.update) {             /* if mouse registered activity..    */
  130.          msdata.update = 0;            /* reset the mouse activity flag     */
  131.          prdata.fgcolor = YELLOW;      /* set the foreground text color     */
  132.          prdata.command = 0;           /* specify text to be centered       */
  133.          prdata.x = 0, prdata.y = LOC3;     /* define the location to print */
  134.          sprintf((prdata.string2print = buffer), "%s %d, %d  ",
  135.                                    text[MSG0B], msdata.pos[0], msdata.pos[1]);
  136.          hidems();                    /* hide mouse cursor, updating screen */
  137.          print();                      /* print cursor location             */
  138.          prdata.command = -1;          /* specify no modifications          */
  139.          prdata.y = LOC1;              /* define y-coordinate of next print */
  140.          if (!msdata.buts[0]) {        /* if left button is not pressed..   */
  141.             prdata.string2print = text[MSG03];
  142.             print();                   /* print left button is not pressed  */
  143.             sprintf((prdata.string2print = buffer), "%s %d, %d  ",
  144.                                text[MSG06], msdata.butlr[0], msdata.butlr[1]);
  145.             prdata.y++;                /* increment y-coordinate of print   */
  146.          }
  147.          else {                        /* else left button is pressed..     */
  148.             prdata.string2print = text[MSG04];
  149.             print();                   /* print left button is pressed      */
  150.             sprintf((prdata.string2print = buffer), "%s %d, %d  ",
  151.                                text[MSG05], msdata.butlp[0], msdata.butlp[1]);
  152.             prdata.y += 2;             /* add to y-coordinate of print      */
  153.          }
  154.          print();                      /* print left button coordinates     */
  155.          prdata.y = LOC2;              /* define new y-coordinate for print */
  156.          if (!msdata.buts[1]) {        /* if right button is not pressed..  */
  157.             prdata.string2print = text[MSG07];
  158.             print();                   /* print right button is not pressed */
  159.             sprintf((prdata.string2print = buffer), "%s %d, %d  ",
  160.                                text[MSG0A], msdata.butrr[0], msdata.butrr[1]);
  161.             prdata.y++;                /* increment y-coordinate of print   */
  162.          }
  163.          else {                        /* else right button is pressed..    */
  164.             prdata.string2print = text[MSG08];
  165.             print();                   /* print right button is pressed     */
  166.             sprintf((prdata.string2print = buffer), "%s %d, %d  ",
  167.                                text[MSG09], msdata.butrp[0], msdata.butrp[1]);
  168.             prdata.y += 2;             /* add to y-coordinate of print      */
  169.          }
  170.          print();                      /* print right button coordinates    */
  171.          showms();                     /* show mouse cursor, done updating  */
  172.       }
  173.    }
  174.    changevideomode(oldmode);           /* restore the original video mode   */
  175.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  176.                                        /* if the old mode was a text mode.. */
  177.       textmem(1);                      /* restore text video memory         */
  178.       cursor(1, 1);                    /* show and restore cursor position  */
  179.    }
  180. }
  181.  
  182. /*
  183.  *  32 250 249 46 7 176 177 178 219
  184.  *      ·   ∙   .    ░   ▒   ▓   █
  185.  */
  186. void floodtext(void)
  187. #define FLOODCHARNO 8
  188. {
  189.    static int cycle = 1, dir = 1,
  190.               data[] = { 32, 250, 249, 46, 7, 176, 177, 178, 219 };
  191.  
  192.    if (stopw(0, 0) == TRUE) {
  193.       fidata.fillchar = data[cycle];   /* set the character to fill with    */
  194.       fidata.char2overwrite = data[cycle - dir];   /* set char to overwrite */
  195.       if (cycle == FLOODCHARNO || (cycle == 1 && dir < 0))
  196.          dir = -dir;        /* start cycling the fill characters in reverse */
  197.       cycle += dir;                    /* get the next fill character       */
  198.       hidems(); fillarea(); showms();  /* fill screen with the character    */
  199.       msdata.update++;                 /* reprint the mouse status onscreen */
  200.       showtext();                      /* reprint the standard text         */
  201.       stopw(0, 15);                    /* reset stopwatch 0                 */
  202.    }
  203. }
  204.  
  205. /*
  206.  * PRINTS INITIAL TEXT
  207.  */
  208. void showtext(void)
  209. {
  210.    prdata.fgcolor = WHITE, prdata.bgcolor = BLUE;    /* set printing colors */
  211.    prdata.command = 0;                 /* center the text to print          */
  212.    prdata.x = 0, prdata.y = LOC4;      /* define the coordinates to print   */
  213.    prdata.string2print = text[MSG01];
  214.    print();                            /* print first line of title         */
  215.    prdata.y++;                         /* increment the y-coordinate        */
  216.    prdata.string2print = text[MSG02];
  217.    print();                            /* print second line of title        */
  218.    prdata.y = LOC5;                    /* define the y-coordinate to print  */
  219.    prdata.string2print = text[MSG0C];
  220.    print();                            /* print the "any key exits" message */
  221. }
  222.